package com.controller;

/**
 * Created by wlc on 2018/9/10.
 */

import com.entity.UserFile;
import com.entity.UserMetadata;
import com.service.UserFileOperator;
import com.util.CookieUtil;
import com.util.LocalInformation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * 映射用户文档管理部分
 */
@Controller
@RequestMapping(value = "/fileManager",produces = "text/html;charset=UTF-8")
public class FileManagerController {
    //映射到文档管理界面
    @RequestMapping(value = "/UserFiles",produces = "text/html;charset=UTF-8")
    public ModelAndView UserFiles(HttpServletRequest request) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());

            ArrayList<UserFile> userFiles = UserFileOperator.getAllFilesContent(user_cookie_value.split("wlcniubi")[1]);

            modelAndView.addObject("userFiles",userFiles);
            modelAndView.setViewName("User-fileManager");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到文档编写界面
    @RequestMapping(value = "/WriteFiles",produces = "text/html;charset=UTF-8")
    public ModelAndView WriteFiles(HttpServletRequest request) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());

            modelAndView.setViewName("template-fileWrite");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到文档提交部分
    @RequestMapping(value = "/submitFile",produces = "text/html;charset=UTF-8")
    public ModelAndView submitFile(HttpServletRequest request,
                                   @RequestParam(value = "id")String id,
                                   @RequestParam(value = "title")String title,
                                   @RequestParam(value = "type")String type,
                                   @RequestParam(value = "content")String content) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());

            UserFileOperator.deleteFile(id,true);
            UserFileOperator.submitUserFile(user_cookie_value.split("wlcniubi")[1],title,type,content);
            ArrayList<UserFile> userFiles = UserFileOperator.getAllFilesContent(user_cookie_value.split("wlcniubi")[1]);

            modelAndView.addObject("userFiles",userFiles);
            modelAndView.setViewName("User-fileManager");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到文档保存部分
    @RequestMapping(value = "/saveFile",produces = "text/html;charset=UTF-8")
    public void saveFile(HttpServletRequest request) throws Exception{

    }

    //映射到文档删除部分
    @RequestMapping(value = "/fileDelete",produces = "text/html;charset=UTF-8")
    public ModelAndView fileDelete(HttpServletRequest request, @RequestParam(value = "fileId")String fileId) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());


            UserFileOperator.deleteFile(fileId,false);
            ArrayList<UserFile> userFiles = UserFileOperator.getAllFilesContent(user_cookie_value.split("wlcniubi")[1]);

            modelAndView.addObject("userFiles",userFiles);
            modelAndView.setViewName("User-fileManager");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到文档还原部分
    @RequestMapping(value = "/fileReduction",produces = "text/html;charset=UTF-8")
    public ModelAndView fileReduction(HttpServletRequest request, @RequestParam(value = "fileId")String fileId) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());

            UserFileOperator.reductionFile(fileId);
            ArrayList<UserFile> userFiles = UserFileOperator.getAllFilesContent(user_cookie_value.split("wlcniubi")[1]);

            modelAndView.addObject("userFiles",userFiles);
            modelAndView.setViewName("User-fileManager");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到文档编辑部分
    @RequestMapping(value = "/fileEdit",produces = "text/html;charset=UTF-8")
    public ModelAndView fileEdit(HttpServletRequest request, @RequestParam(value = "fileId")String fileId) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
        if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
            UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());


            UserFile userFile = UserFileOperator.getFileContent(fileId);

            modelAndView.addObject("userFile",userFile);
            modelAndView.setViewName("template-fileWrite");
        }else {
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }

    //映射到文档显示部分,通过判断是否需要用户登录进行相应显示
    @RequestMapping(value = "/fileShow",produces = "text/html;charset=UTF-8")
    public ModelAndView fileShow(HttpServletRequest request, @RequestParam(value = "fileId")String fileId) throws Exception{
        ModelAndView modelAndView = new ModelAndView();

        UserFile userFile = UserFileOperator.getFileContent(fileId);
        if (userFile.getType().contains("公告")){
            modelAndView.addObject("userFile",userFile);
            modelAndView.setViewName("template-announcement");
        }else {
            String user_cookie_value = CookieUtil.getUserCookie(request).getValue();
            if (user_cookie_value != null && UserMetadata.OnlineUserId.containsKey(user_cookie_value)){
                UserMetadata.OnlineUserOperatorTime.replace(user_cookie_value, LocalInformation.getTimeID());

                modelAndView.addObject("userFile",userFile);
                modelAndView.setViewName("template-fileContent");
            }else {
                modelAndView.setViewName("login");
            }
        }

        return modelAndView;
    }
}
